package com.crazy.crazybusinessservice.controller;
import com.crazy.crazybusinessservice.service.BankCardBalanceRecordService;
import com.crazy.crazycommon.common.BaseResponse;
import com.crazy.crazybusinessservice.service.BankCardBalanceService;
import com.crazy.crazycommon.common.ErrorCode;
import com.crazy.crazycommon.common.ResultUtils;
import com.crazy.crazymodel.model.dto.trade.BankRecordRequest;
import com.crazy.crazymodel.model.entity.BankCardBalance;
import com.crazy.crazymodel.model.entity.BankCardWaterRecord;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/bankCardBalance")
@Slf4j
public class BankCardBalanceController {
    @Resource
    private BankCardBalanceService balanceService;
    @Resource
    private BankCardBalanceRecordService recordService;

    /**
     * 按银行卡号进行查询余额
     * @param cardId
     * @return
     */
    @PostMapping ("/getById")
    public BaseResponse<BankCardBalance> getById(String cardId){
        BankCardBalance balance = balanceService.getById(cardId);
        return ResultUtils.success(balance);
    }

    /**
     * 添加新的银行卡余额信息
     * @param balance
     * @return
     */
    @PostMapping("/addBalance")
    public BaseResponse<String> addBalance(@RequestBody BankCardBalance balance){
        BigDecimal db = new BigDecimal(Math.random() * (999999 - 10000) + 10000);
        balance.setBalance(db);
        balance.setFreezeBalance(BigDecimal.ZERO);
        Boolean result =  balanceService.save(balance);
        if(result){
            return ResultUtils.success("save success");
        }else {
            return ResultUtils.error(ErrorCode.OPERATION_ERROR);
        }
    }

    /**
     * 按银行卡号进行删除
     * @param cardId
     * @return
     */
    @PostMapping("/deleteById")
    public BaseResponse<String> deleteById(String cardId){
        Boolean result =  balanceService.removeById(cardId);
        if(result){
            return ResultUtils.success("delete success");
        }else {
            return ResultUtils.error(ErrorCode.OPERATION_ERROR);
        }
    }

    /**
     * 按照银行卡号和时间进行查询流水记录
     * @param request
     * @return
     */
    @PostMapping("getRecordById")
    public BaseResponse<List<BankCardWaterRecord>> getWaterById(@RequestBody BankRecordRequest request){
        List<BankCardWaterRecord> result = null;
        if(request.getStart()!=null && request.getEnd() == null){//有起始时间
            result = recordService.selectByTime(request.getBankCardId(), request.getStart());
        }else if(request.getStart()!=null && request.getEnd() != null){//有起始时间和结束时间
            result = recordService.selectByTime(request.getBankCardId(),request.getStart(),request.getEnd());
        }
        if(result ==null){
            return ResultUtils.error(ErrorCode.OPERATION_ERROR);
        }else {
            return ResultUtils.success(result);
        }
    }
}
